package com.dms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.dms.bean.Fee;
import com.dms.util.DBUtil;

public class FeeDao {
	private static DBUtil	db	= new DBUtil();
	public static boolean add(Fee fee) {
		boolean val = false;
		if (fee!=null) {
			String sql = "insert into fee (month,dormID,money,state) values(?,?,?,?)";
			try {
				db.update(sql,fee.getMonth(), fee.getDormID(), fee.getMoney(), 0);
				val=true;
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			try {
				db.close();
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return val;
	}
	
	public static ArrayList<Fee> list() {
		ArrayList<Fee> fees = new ArrayList<Fee>();
		String sql = "select * from fee order by month";
		try {
			ResultSet rs = db.query(sql);
			while (rs.next()) {
				fees.add(new Fee(rs.getInt("feeID"),rs.getInt("month"),rs.getString("dormID"),rs.getString("money"),rs.getInt("state")));
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		try {
			db.close();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		return fees;
	}
	
	public static Fee find(int feeID) {
		boolean val = false;
		Fee fee = new Fee();
		if (feeID != 0) {
			String sql = "select * from fee where feeID=?";
			ResultSet rs = null;
			try {
				rs = db.query(sql, feeID);
				val = (rs != null && rs.next()) ? true : false;
				if (val) {
					fee.setFeeID(rs.getInt("feeID"));
					fee.setMonth(rs.getInt("month"));
					fee.setDormID(rs.getString("dormID"));
					fee.setMoney(rs.getString("money"));
					fee.setState(rs.getInt("state"));
				}
			}
			catch (Exception e) {
				val = false;
				e.printStackTrace();
			}
			try {
				db.close();
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return fee;
	}
	
		public static boolean save(Fee fee) throws Exception {
			boolean var = false;
			try {
				if (fee != null) {
					String sql = "update fee set month=?,dormID=?,money=?,state=? where feeID =?";
					db.update(sql, fee.getMonth(), fee.getDormID(), fee.getMoney(), fee.getState(), fee.getFeeID());
					var = true;
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			try {
				db.close();
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
			return var;
		}
		public static boolean delete(int feeID) {
			boolean var = false;
			try {
					String sql = "delete from fee where feeID =?";
					db.update(sql, feeID);
					var = true;
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			try {
				db.close();
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
			return var;
		}
		
		public static ArrayList<Fee> list2(String dormID) {
			ArrayList<Fee> fees = new ArrayList<Fee>();
			String sql = "select * from fee where dormID= '"+dormID+"' order by month";
			try {
				ResultSet rs = db.query(sql);
				while (rs.next()) {
					fees.add(new Fee(rs.getInt("feeID"),rs.getInt("month"),rs.getString("dormID"),rs.getString("money"),rs.getInt("state")));
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			try {
				db.close();
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
			return fees;
		}
}
